package com.lanswon.qzsmk.controller.appManageController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.AppClassification;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.service.appManageService.AppOpenRegService;
import com.lanswon.qzsmk.service.appManageService.ApplicationTypeService;
import com.lanswon.qzsmk.service.appManageService.impl.ApplicationTypeServiceImpl;
import com.lanswon.qzsmk.vo.ApplicationDefVo;
import com.lanswon.qzsmk.vo.ApplicationTypeVo;
import com.lanswon.qzsmk.vo.ResponseCardFileVo;
import com.lanswon.qzsmk.vo.SaveOrUpdateTellerVo;
import com.lanswon.qzsmk.vo.VoucherVo;

import ch.qos.logback.classic.Logger;
/**
 * @author hgl
 *
 */

@Controller
@RequestMapping(value="/applicationDefinition")
public class ApplicationDefinitionController{

	@Autowired
	// 卡应用开通映射
	AppOpenRegService ac;
	@Autowired
	ApplicationTypeService   ats;
	
	private final static Logger logger = (Logger) LoggerFactory.getLogger(ApplicationDefinitionController.class);
	
	/**
	 * 应用定义主页面
	 * @return
	 */
	@RequiresPermissions("applicationDefinition:index")
	@RequestMapping(name="应用定义主页面,applicationDefinition:index",value = "/toAppDef", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryAppType() {
		return "ApplicationDefinition";
	}
	
	/**
	 * 动态获取应用类型分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getAppKindName", method = RequestMethod.GET)
	@ResponseBody
	@SystemControllerLog(description = "加载应用分类信息下拉框")
	public Map<String, Object> getAppKindName() throws Exception{
		List<AppClassification> sdd = ac.queryAppClassificationsKindname();
		Map<String, Object> map = new HashMap<>();
		map.put("sdd", sdd);
		return map;
	}
	
	/**
	 * 查询应用定义信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("applicationDefinition:queryAppDefinition")
	@RequestMapping(name="查询应用定义,applicationDefinition:queryAppDefinition",value="/queryAppDefinition",method=RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "初始化页面表格栏应用信息")
	public Map<String,Object> queryType(HttpServletRequest request) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		DwzPage<ApplicationDefVo> dPage = ats.queryAppKindReg(request);
		map.put("dPage", dPage);
		logger.debug("map==>"+map);
		return map;	
	}
	
	/**
	 * 增加应用定义页面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("applicationDefinition:addAppKind")
	@RequestMapping(name="增加应用定义页面,applicationDefinition:addAppKind",value="/addAppKind",method=RequestMethod.GET)
	public String addAppKind(HttpServletRequest request){
		request.getSession().setAttribute("sign", true);
		return "AddNewAppKind";
	}
	
	/**
	 * 修改应用定义页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("applicationDefinition:modifyAppKind")
	@RequestMapping(name="跳转修改应用定义页面,applicationDefinition:modifyAppKind",value="/modifyAppKind",method=RequestMethod.GET)
	@SystemControllerLog(description = "修改应用定义页面")
	public String modifyAppKind(HttpServletRequest request) throws Exception{
		request.getSession().setAttribute("sign", "modify");
		Integer appType = Integer.parseInt(request.getParameter("appType"));
		String pageFlag = request.getParameter("pageFlag");
		if("detail".equals(pageFlag)){
			request.getSession().setAttribute("sign", "detail");
		}
		AppKind appKindObj = ats.queryAppKindInfo(appType);
		logger.debug("appKindObj==>"+appKindObj);
		request.getSession().setAttribute("appKind", appKindObj);
		return "AddNewAppKind";
	}
	
	/**
	 * 获取将要修改的应用信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("applicationDefinition:getAppKindInfoByModify")
	@RequestMapping(name="获取将要修改的应用信息,applicationDefinition:getAppKindInfoByModify",value="/getAppKindInfoByModify",method=RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "修改应用定义页面")
	public Map<String, Object> getAppKindInfoByModify(HttpServletRequest request)throws Exception{
		Map<String, Object> map=new HashMap<>();
		map.put("sign",request.getSession().getAttribute("sign"));
		map.put("appKind",request.getSession().getAttribute("appKind"));
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		map.put("activeUser", activeUser);
		request.getSession().removeAttribute("sign");
		request.getSession().removeAttribute("appKind");
		return map;	
	}
	
	/**
	 * 获取账户信息
	 * @return
	 */
	@RequestMapping(value="/getActiveUserInfo",method=RequestMethod.GET)
	@ResponseBody
	@SystemControllerLog(description = "获取账户信息")
	public Map<String, Object> getActiveUserInfo(){
		Map<String, Object> map=new HashMap<>();
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		map.put("activeUser", activeUser);
		return map;	
	}
	
	/**
	 * 根据名称查询应用
	 * @param request
	 * @return
	 */
	/*@RequiresPermissions("applicationDefinition:queryAppKindInfoByAppName")
	@RequestMapping(name="根据名称查询应用,applicationDefinition:queryAppKindInfoByAppName",value="/queryAppKindInfoByAppName",method=RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "验证应用名称是否重复")
	public Map<String, Object> queryAppKindInfoByAppName(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		map = ats.queryAppKindInfoByAppName(request);
		return map;	
	}*/
	
	/**
	 * 登记新增应用
	 * @param appKind
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("applicationDefinition:addAppDefToDB")
	@RequestMapping(name="登记新增应用,applicationDefinition:addAppDefToDB",value="/addAppDefToDB",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAppDefToDB(AppKind appKind) throws Exception{
		Map<String, Object> map=new HashMap<>();
		
		Map<String, Object> resultMap=new HashMap<>();
		//先查询是否有重复得应用名称，如果有，提示重复信息
		resultMap = ats.addAppDef(appKind);
		//如果没有，提交数据库
	    if((boolean) resultMap.get("flag")){
	    	map.put("statusCode", "200");
			map.put("navTabId", "ApplicationDefinition");
			map.put("callbackType", "closeCurrent");
			map.put("forwardUrl", "applicationDefinition/toAppDef");//post请求
	    }else{
	    	map.put("statusCode", "300");
			map.put("message", "应用名称或应用标识不能重复，请重新输入");
	    }
		return map;

	}
	
	/**
	 * 登记被修改的应用信息
	 * @param appKind
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("applicationDefinition:updateAppDefToDB")
	@RequestMapping(name="登记被修改的应用信息,applicationDefinition:updateAppDefToDB",value="/updateAppDefToDB",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateAppDefToDB(AppKind appKind) throws Exception{
		Map<String, Object> map=new HashMap<>();
		
		Map<String, Object> resultMap=new HashMap<>();
		//先查询是否有重复得应用名称，如果有，提示重复信息
		ats.updateAppDef(appKind);
		//如果没有，提交数据库
	    map.put("statusCode", "200");
		map.put("navTabId", "ApplicationDefinition");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "applicationDefinition/toAppDef");//post请求

		return map;

	}	
}
